package controller;

import java.io.IOException;

import dao.WeiGuiDao;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import model.WeiGui;

/**
 * Servlet implementation class addUserServlet
 */
@WebServlet("/AddWeiGuiServlet")
public class AddWeiGuiServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String id = request.getParameter("id");
        String time = request.getParameter("time");
        String type = request.getParameter("type");
        String because = request.getParameter("because");
        String address = request.getParameter("dormNumber");
//     测试
        System.out.println("AddWeiGuiServlet id:"+id+"  time:"+time+"  type:"+type+" because:"+because+"roomid"+address);

        WeiGui wg = new WeiGui(id, time, type, because,address);
        WeiGuiDao dao = new WeiGuiDao();
        	
        
        boolean tip = dao.addMsg(wg);
    	

        // 如果添加用户成功，转发到userlList.jsp，列出所有用户
        if (tip) {
            // 验证成功，转发到 welcome.jsp
        	response.sendRedirect("View_WeiGui/WeiGuiSG.jsp");
            
        } else {
            // 验证失败，重新回到 login.jsp
            response.sendRedirect("View_WeiGui/WeiGuiSG_add.jsp");
        }
	}

}
